package com.onluy.sysman.controller.sysmgr;


import com.alibaba.fastjson.JSON;
import com.onluy.frameworkcloud.Param.ParamPagination;
import com.onluy.frameworkcloud.Param.ReqHead;
import com.onluy.frameworkcloud.Param.ReqInfo;
import com.onluy.frameworkcloud.Param.RspInfo;
import com.onluy.frameworkcloud.base.AppWalkController;
import com.onluy.frameworkcloud.data.bean.PageData;
import com.onluy.frameworkcloud.data.util.InParam;
import com.onluy.sysman.param.DepartParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import com.onluy.sysman.service.TfMgrDepartService;
import com.onluy.sysman.entity.sysmgr.TfMgrDepart;

@Validated
@RestController
@RequestMapping("/depart")
@Api(value="depart", description="/部门管理接口", position=1 ) 
public class TfMgrDepartController extends AppWalkController {


	//调用服务
	@Autowired
	private TfMgrDepartService tfMgrDepartService;

	@RequestMapping(value={"/target"},method={ RequestMethod.POST})
	@ApiOperation(value="目标", notes="根据departCode,或 id 获取信息")
	public RspInfo<TfMgrDepart> dotarget(@RequestBody ReqInfo<ReqHead, DepartParam> reqInfo   ) throws Exception {
		RspInfo<TfMgrDepart> jsonResult = new RspInfo<TfMgrDepart>();
		log.info("------------>Model reqInfo : " + JSON.toJSONString(reqInfo));
		TfMgrDepart tfMgrDepart = new TfMgrDepart();

		if (null !=  reqInfo.getReqBody().getDepartCode()) {
			tfMgrDepart.setDepartCode(reqInfo.getReqBody().getDepartCode()).asCondition();
		}
		else{
			if( null == reqInfo.getReqBody().getId() )throw new Exception("缺少必要属性userId！");

			tfMgrDepart.setId(reqInfo.getReqBody().getId()).asCondition();
		}
		tfMgrDepart = tfMgrDepartService.model(tfMgrDepart);
		jsonResult.setRspCode(0);
		jsonResult.setRspData(tfMgrDepart);
		return jsonResult;
	}



	@RequestMapping(value={ "/add"},method={RequestMethod.POST })
	@ApiOperation(value="新增", notes="新增部门")
	public RspInfo<InParam> doAdd(@RequestBody @Valid ReqInfo<ReqHead,TfMgrDepart> reqInfo )  throws Exception{
		RspInfo<InParam> jsonResult = new RspInfo<InParam>();
		int num = 0;
		TfMgrDepart tfMgrDepart = reqInfo.getReqBody();
		num = tfMgrDepartService.add(tfMgrDepart);
		if (num > 0) {
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("增加成功！");
		} else {
			jsonResult.setRspDesc("增加失败！");
		}
		return jsonResult;
	}

	@RequestMapping(value={ "/update"},method={RequestMethod.POST})
	@ApiOperation(value="更新", notes="根据userId更新用户")
	public RspInfo<InParam> doUpdate(      @RequestBody @Valid  ReqInfo<ReqHead,TfMgrDepart> reqInfo ) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		int num=0;

		if( null == reqInfo.getReqBody().getId() )throw new Exception("缺少必要属性id！");

		TfMgrDepart tfMgrDepart = reqInfo.getReqBody();
		tfMgrDepart.setId(  reqInfo.getReqBody().getId()  ).asCondition();
		num=tfMgrDepartService.update( tfMgrDepart);
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("修改成功！");
		}
		else {
			jsonResult.setRspDesc("修改失败！");
		}
		return jsonResult;
	}




	@RequestMapping(value={"/del"},method={RequestMethod.POST})
	@ApiOperation(value="删除", notes="根据id删除用户")
	public RspInfo<InParam> doDel( @RequestBody    ReqInfo<ReqHead,DepartParam> reqInfo) throws Exception {
		RspInfo<InParam> jsonResult=new RspInfo<InParam>();
		TfMgrDepart tfMgrDepart = new TfMgrDepart();
		int num=1;
		try {
			if( null == reqInfo.getReqBody().getId() )throw new Exception("缺少必要属性userId！");

			tfMgrDepart.setId(  reqInfo.getReqBody().getId()  ).asCondition();

			num=tfMgrDepartService.del( tfMgrDepart);
		} catch (Exception e) {
			jsonResult.setRspDesc("删除异常:"+e.getMessage());
			e.printStackTrace();
		}
		if(  num>0 ){
			jsonResult.setRspCode(0);
			jsonResult.setRspDesc("删除成功！");
		}
		else {
			jsonResult.setRspDesc("删除失败！");
		}
		return jsonResult;
	}


	@ResponseBody
	@RequestMapping(value={ "/list"},method={RequestMethod.POST })
	@ApiOperation(value="列表", notes="部门列表明细")
	public RspInfo<PageData<TfMgrDepart>>  doList(@RequestBody    ReqInfo<ParamPagination, TfMgrDepart> reqInfo )throws Exception {
		RspInfo<PageData<TfMgrDepart>> jsonResult=new RspInfo<PageData<TfMgrDepart>>();

		this.MakeLikeEntity(  reqInfo.getReqBody());//处理查询条件

		PageData<TfMgrDepart> list =tfMgrDepartService.list( reqInfo.getReqBody(),FormatPagination(reqInfo.getReqHead()));
		jsonResult.setRspCode(0);
		jsonResult.setRspData(list);
		return jsonResult;
	}

	public  void  MakeLikeEntity(TfMgrDepart tfMgrDepart){

		if(tfMgrDepart == null )return;

		if(null!=tfMgrDepart.getDepartCode()){
			tfMgrDepart.setDepartCode(tfMgrDepart.getDepartCode()).asCondition();
		}
		if(null!=tfMgrDepart.getDepartName()){
			tfMgrDepart.setDepartName(null).getCondition().andLike( "%"+tfMgrDepart.getDepartName()+"%" );
		}

	}
		

}